package com.example.autocreatenotesdemo.service;

import cn.hutool.core.io.LineHandler;
import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.io.file.FileWriter;
import cn.hutool.core.util.URLUtil;
import com.example.autocreatenotesdemo.exception.BusinessException;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author susc
 * @title: CreateNoteImpl
 * @projectName auto-create-notes-demo
 * @description: TODO
 * @date 2022/1/79:16
 */
@Service
@Slf4j
public class CreateNoteImpl implements ICreateNote{




    @Override
    public List<String> analysisApiModelProperty(List<String> rowList) {
        List<String> newRowList = new ArrayList<>();
        Iterator<String> iterator = rowList.iterator();
        while (iterator.hasNext()){
            String row = iterator.next();
            boolean isApiModelProperty =  row.contains("@ApiModelProperty(");
            // 判断是否已存在注释
            String preRow = newRowList.size() > 1 ?newRowList.get(newRowList.size() - 1):"";
            boolean isNotExistNote = !preRow.contains("*/");
            // 判断是否有@ApiModelProperty注解
            if(isApiModelProperty && isNotExistNote){
                String apiModelProperty = getApiModelPropertyValue(row);

                StringBuffer noteStringBuffer = new StringBuffer();
                noteStringBuffer.append("    /**").append("\n").append("     * ").append(apiModelProperty).append("\n")
                        .append("     */").append("\n").append(row);
                row = noteStringBuffer.toString();
            }
            newRowList.add(row);
        }
        return newRowList;
    }

    private String getApiModelPropertyValue(String row) {
        String apiModelProperty = row.replace("@ApiModelProperty(", "").replace(")", "").replace("\"", "").
        trim();
        // 读取value=“” 内容
        String[] split = apiModelProperty.split(",");
        for (String s : split) {
            // 去空格
            s = s.trim();
            boolean contains = s.contains("value =") || s.contains("value=");
            if(contains){
                apiModelProperty = s.replace("value =", "").replace("value=", "");
                break;
            }
        }
        return apiModelProperty;
    }

    @Override
    public String createNote(String path) throws IOException {
        String decodePath = URLUtil.decode(path);
        File file = new File(decodePath);
        if(file.isFile()){
            FileReader fileReader = FileReader.create(file, StandardCharsets.UTF_8);
            List<String> strings = fileReader.readLines();
            // 判断哪一行有ApiModelProperty注解
            List<String> newString = analysisApiModelProperty(strings);
            StringBuffer noteBuffer = new StringBuffer();
            newString.forEach(
                    noteItem->{
                        noteBuffer.append(noteItem).append("\n");
                    }
            );
            // 在同路径下创建新的文件
            FileWriter fileWriter = FileWriter.create(file);
            fileWriter.write(noteBuffer.toString());
        }else{
            throw new BusinessException("文件类型异常!");
        }
        log.info("解析成功");
        return null;
    }
}
